package com.lin.server.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义线程工厂，用于线程池创建线程
 */
public class CusThreadFactory implements ThreadFactory {
    private boolean daemon;
    private final AtomicInteger threadNumber = new AtomicInteger(1);

    public CusThreadFactory(boolean daemon) {
        this.daemon = daemon;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName("schedule-pool-" + threadNumber.getAndIncrement());
        thread.setDaemon(this.daemon);
        return thread;
    }
}
